package com.test.template.tool;

import org.apache.velocity.app.VelocityEngine;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;


/**
 * description 配置类，管理Velocity的配置
 *
 * @author zken
 * @CreateDate 2024/11/26 19:51:00
 */
public class VelocityConfig {

    /**
     * description 获取一个模板引擎
     *
     * @author zken
     * @CreateDate 2024/11/26 19:51:17
     */
    public static VelocityEngine getVelocityEngine() {
        // 初始化 Velocity 引擎
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty("file.resource.loader.path", "src/test/resources/templates");
        velocityEngine.init();
        return velocityEngine;
    }


    // 输出路径
    static String generatePath = "src/test/java/com/test/template/generate";

    /**
     * description 输出StringWriter为文件
     *
     * @author zken
     * @CreateDate 2024/11/26 19:51:36
     */
    public static void output(StringWriter writer) {
        try (
                FileOutputStream fos = new FileOutputStream(new File(generatePath + "/test.html"));
        ) {
            fos.write(writer.toString().getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * description 输出StringWriter为文件
     *
     * @author zken
     * @CreateDate 2024/11/26 19:51:36
     */
    public static void output(StringWriter writer,String generateFilePath) {
        try (
                FileOutputStream fos = new FileOutputStream(new File(generateFilePath));
        ) {
            fos.write(writer.toString().getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
